// pages/ticketsChoose/ticketsChoose.js
let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        titleArr: ['代金券', '满减券', '商品券', '折扣券'],
        headTitle: '',
        show: false,
        content: [],
        selectedContent: [],
        type: '',
        selectedStatus: false,
        multiStatus: false,
        extra: {}
        // delivery: null
    },
    navShowT(e){
      let uri = ``
      if(this.data.type == 'choose'){
        uri = `&hideDel=true`
      }
      let { item } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/ticketsCreateDetail/ticketsCreateDetail?obj=${JSON.stringify(item)}&type=${item.type.substr(1)}` + uri,
      })
    },
    clickContainer(e){
        if (e.target.id == e.currentTarget.id){
            this.setData({
                show: false
            })
        }
    },
    remove(e){
        const delIndex = e.currentTarget.dataset.index
        const delId = e.currentTarget.dataset.id
        this.data.selectedContent.splice(delIndex, 1) 
        this.setData({
            selectedContent: this.data.selectedContent
        })
        this.data.content.filter(val => val.couponPoolId == delId)[0]['active'] = false
        this.setData({
            content: this.data.content
        })
    },
    selectFun(e){
        const item = e.currentTarget.dataset.item
        const index = e.currentTarget.dataset.index
        if (this.data.content[index]['active']){
            this.data.content[index]['active'] = false
            let delIndex = this.data.selectedContent.findIndex(val => val.couponPoolId == item.couponPoolId)
            this.data.selectedContent.splice(delIndex, 1)
        }
        else{
            this.data.content[index]['active'] = true
            this.data.selectedContent.push(item)
        }
        this.setData({
            content: this.data.content,
            selectedContent: this.data.selectedContent
        })
    },
    confirm(){
        if (!this.data.selectedContent.length){
            wx.showModal({
                title: '提示',
                content: '至少选择一个卡券',
                showCancel: false
            })
            return
        }
        let idArray = this.data.selectedContent.map(val => val.couponPoolId)
        app.globalData.post({
            url: '/couponGroup/create',
            data: {
                name: 'asdf',
                validityLowerBound: '2000-12-12',
                validityUpperBound: '2037-12-31',
                deliverRules: '{"asdf": 1}',
                couponPoolIds: JSON.stringify(idArray)
            },
            success: (data) => {
                console.log(data)
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setCouponPool({
                    couponPoolId: data.data,
                    couponPoolIds: idArray,
                    extra: this.data.extra
                })
                wx.navigateBack({
                    
                })
                // ({
                //     url: `/pages/collectCard/collectCard?couponPoolId=${data.data}&couponPoolIds=${JSON.stringify(idArray)}&delivery=${this.data.delivery}`,
                // })
            }
        })
    },
    choose: function (e) {
        // sent data change to view
        if (this.data.show === false) {
            this.setData({
                show: true
            })
        } else {
            this.setData({
                show: false
            })
        }
        // console.log(this.data.show);
    },
    cMulti(){
        this.setData({
            selectedStatus: !this.data.selectedStatus
        })
    },
    cDel(){
        if (!this.data.selectedContent.length){
            wx.showModal({
                title: '提示',
                content: '请至少选择一个',
                showCancel: false
            })
            return
        }
        wx.showModal({
            title: '提示',
            content: '确认删除？',
            success: (res) => {
                if (res.confirm) {
                  let ids = this.data.content.filter(val => val.active).map(val => val.couponPoolId)
                    app.globalData.post({
                        url: '/couponPool/delCouponPool',
                        data: {
                            couponPoolIds: JSON.stringify(ids)
                        },
                        success: () => {
                            this.setData({
                                content: this.data.content.filter(val => !val.active)
                            })
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    navCT(){
        wx.navigateTo({

            url: `/pages/ticketsCreateDetail/ticketsCreateDetail?type=${this.data.type.substr(1)}`
        })
    },
    init(options){
        let type = options.type
        this.setData({
            type
        })
        if (type == 'choose') {
            this.setData({
                headTitle: '全部券',
                extra: options.extra
            })
            
            let couponPoolIds = JSON.parse(options.couponPoolIds)
            this.setData({
                selectedStatus: true
            })
            // this.setData({
            //     delivery: options.delivery
            // })
            app.globalData.post({
                url: '/couponPool/getList',
                data: {
                    pageNum: 0,
                    pageSize: 10000,
                    isExpire: 0,
                    type: ''
                },
                success: (data) => {
                    data.data.forEach(val => {
                      val.couponPoolId = val.id
                    })
                    this.setData({
                        content: data.data
                    })
                    // if(type == 'choose'){
                    couponPoolIds.forEach(id => {
                      this.data.content.filter(val => val.couponPoolId == id)[0].active = true

                    })
                    this.setData({
                        content: this.data.content,
                        selectedContent: this.data.content.filter(val => val.active)
                    })
                    // }
                }
            })
        }
        else {
            this.setData({
                multiStatus: true
            })
            if (type == 'collect') {
                let content
                this.setData({
                    headTitle: '集点卡'
                })
                app.globalData.post({
                    url: '/pointCard/getList',
                    data: {
                        pageNum: 0,
                        pageSize: 1000,
                        isExpire: 2
                    },
                    success: (data) => {
                        this.setData({
                            content: data.data
                        })
                    }
                })
            }
            else {
                let content, headTitle
                console.log(type)
                app.globalData.post({
                    url: '/couponPool/getList',
                    data: {
                        pageNum: 0,
                        pageSize: 1000,
                        type: `[${type.substr(1)}]`,
                        isExpire: 2
                    },
                    success: (data) => {
                        this.setData({
                            content: data.data
                        })
                    }
                })
                if (type == 'at') {
                    headTitle = '全部券'
                }
                else {
                    headTitle = this.data.titleArr[+options.type.substr(1) - 1]
                }
                this.setData({
                    headTitle
                })
            }


        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.init(options)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})